package controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import pojo.Comment;
import service.PlayerBusiness;
import service.ServiceFactory;
import util.JsonParser;

@Controller
public class CommentTransfer {

	private PlayerBusiness playerBusiness = ServiceFactory.getPlayBusiness();
	private JsonParser jsonParser = new JsonParser();

	@ResponseBody
	@RequestMapping(value = "/getComment/{songId}", method = { RequestMethod.GET },produces = "application/json;charset=UTF-8")
	public String getComment(@PathVariable("songId") int songId) {
		String json = playerBusiness.getComment(songId);
		return json;
	}

	@ResponseBody
	@RequestMapping(value = "/addComment", method = { RequestMethod.POST },produces = "application/json;charset=UTF-8")
	public String addComment(@RequestBody String commentJson) {
		Comment comment = jsonParser.parseComment(commentJson);
		Date date = new Date();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		comment.setCreatedTime(simpleDateFormat.format(date));
		String json = playerBusiness.addComment(comment);
		return json;
	}

	@ResponseBody
	@RequestMapping(value = "/deleteComment/{commentId}", method = { RequestMethod.GET },produces = "application/json;charset=UTF-8")
	public String deleteComment(@PathVariable("commentId") int commentId) {
		String json = playerBusiness.deleteComment(commentId);
		return json;
	}

}
